<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="货源ID" prop="basicId">
        <el-input
          v-model="queryParams.basicId"
          placeholder="请输入货源ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货源SKU" prop="basicSku">
        <el-input
          v-model="queryParams.basicSku"
          placeholder="请输入货源SKU"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货源平台" prop="basicPlatform">
        <el-select v-model="queryParams.basicPlatform" placeholder="请选择货源平台" clearable>
          <el-option
            v-for="dict in dict.type.basic_platform"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="品牌" prop="brand">
        <el-select v-model="queryParams.brand" placeholder="请选择品牌" clearable>
          <el-option
            v-for="dict in dict.type.brand_list"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="大类目" prop="className">
        <el-select v-model="queryParams.className" placeholder="请选择大类目" clearable>
          <el-option
            v-for="dict in dict.type.amazon_us_class_name"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="小类目" prop="productType">
        <el-input
          v-model="queryParams.productType"
          placeholder="请输入小类目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="修改者" prop="updateBy">
        <el-select v-model="queryParams.updateBy" placeholder="请选择修改者" clearable>
          <el-option
            v-for="dict in dict.type.update_by_name_list"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="排序方式">
        <el-select v-model="sortForm.sort" placeholder="请选择排序方式" @change="handleSortChange">
          <el-option label="按ID升序" value="id,asc"></el-option>
          <el-option label="按ID降序" value="id,desc"></el-option>
          <el-option label="按修改时间升序" value="updateTime,asc"></el-option>
          <el-option label="按修改时间降序" value="updateTime,desc"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="primary"-->
      <!--          plain-->
      <!--          icon="el-icon-plus"-->
      <!--          size="mini"-->
      <!--          @click="handleAdd"-->
      <!--          v-hasPermi="['amazon_us:content:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['amazon_us:content:edit']"
        >修改
        </el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['amazon_us:content:remove']"-->
<!--        >删除-->
<!--        </el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['amazon_us:content:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="ID" align="center" prop="id"/>
      <el-table-column label="货源ID" align="center" prop="basicId"/>
      <el-table-column width="200px" label="货源SKU" align="center" prop="basicSku"/>
      <el-table-column label="货源平台" align="center" prop="basicPlatform">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.basic_platform" :value="scope.row.basicPlatform"/>
        </template>
      </el-table-column>
      <el-table-column label="品牌" align="center" prop="brand">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.brand_list" :value="scope.row.brand"/>
        </template>
      </el-table-column>
      <el-table-column label="大类目" align="center" prop="className">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.amazon_us_class_name" :value="scope.row.className"/>
        </template>
      </el-table-column>
      <el-table-column label="小类目" align="center" prop="productType"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column width="100px" show-overflow-tooltip="true" label="备注" align="center" prop="remark"/>
      <el-table-column label="修改者" align="center" prop="updateBy">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.update_by_name_list" :value="scope.row.updateBy"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['amazon_us:content:edit']"
          >修改
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['amazon_us:content:remove']"-->
          <!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <BackToTop/>
    <BackToBottom/>
    <!-- 添加或修改商品文案对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="货源ID" prop="basicId">
          <el-input v-model="form.basicId" placeholder="请输入货源ID"/>
        </el-form-item>
        <el-form-item label="货源SKU" prop="basicSku">
          <el-input v-model="form.basicSku" placeholder="请输入货源SKU"/>
        </el-form-item>
        <el-form-item label="货源平台" prop="basicPlatform">
          <el-select v-model="form.basicPlatform" placeholder="请选择货源平台">
            <el-option
              v-for="dict in dict.type.basic_platform"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-select v-model="form.brand" placeholder="请选择品牌">
            <el-option
              v-for="dict in dict.type.brand_list"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="大类目" prop="className">
          <el-select v-model="form.className" placeholder="请选择大类目">
            <el-option
              v-for="dict in dict.type.amazon_us_class_name"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="小类目" prop="productType">
          <el-input v-model="form.productType" placeholder="请输入小类目"/>
        </el-form-item>
        <el-form-item label="文案" prop="content">
          <el-input v-model="form.content" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addContent, delContent, getContent, listContent, updateContent} from "@/api/amazon_us/content";
import globalMixins from '@/utils/globalMixins'

export default {
  name: "Content",
  dicts: ['brand_list', 'amazon_us_class_name', 'basic_platform','update_by_name_list'],
  data() {
    return {
      pickerOptions: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品文案表格数据
      contentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 备注时间范围
      daterangeCreateTime: [],
      // 查询参数
      sortForm: {
        sort: 'id,desc' // 默认值
      },
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        basicId: null,
        basicSku: null,
        basicPlatform: null,
        brand: null,
        className: null,
        productType: null,
        createTime: null,
        updateBy: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {}
    };
  },
  created() {
    this.pickerOptions = globalMixins.data().pickerOptions
    this.$set(this.queryParams, 'orderByColumn', 'id');
    this.$set(this.queryParams, 'isAsc', 'desc');
    this.getList();
  },
  methods: {
    handleSortChange(value) {
      const [field, order] = value.split(',');
      // 更新查询参数
      this.$set(this.queryParams, 'orderByColumn', field);
      this.$set(this.queryParams, 'isAsc', order);
      // 根据新的查询参数重新获取列表数据
      this.getList();
    },
    /** 查询商品文案列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && '' != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] = this.daterangeCreateTime[0];
        this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      }
      listContent(this.queryParams).then(response => {
        this.contentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        basicId: null,
        basicSku: null,
        basicPlatform: null,
        brand: null,
        className: null,
        productType: null,
        content: null,
        createTime: null,
        updateTime: null,
        remark: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品文案";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getContent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品文案";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateContent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addContent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除商品文案编号为"' + ids + '"的数据项？').then(function () {
        return delContent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('amazon_us/content/export', {
        ...this.queryParams
      }, `content_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
